using System;
using System.Web;
using System.Web.UI.WebControls;
using SubSonic;
using SurveyHost.Core.DAL;

namespace SurveyHost.Web.Admin
{
    public partial class Questions : System.Web.UI.Page
    {
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            object data = e.NewValues["Rank"];
            int rank;
            if (data == null || !int.TryParse(data as string, out rank) || rank < 1 || rank > 1000)
            {
                e.Cancel = true;
                SetResultText("Rank must be a number between 1 and 1000", null);
            }
        }

        private void SetResultText(string format, params object[] args)
        {
            ResultText.Visible = true;
            ResultText.Text = args == null ? format : string.Format(format, args);
        }

        protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            e.Values["QuestionText"] = HttpUtility.HtmlEncode(e.Values["QuestionText"] as string);
        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            if (e.Exception == null)
                return;
            e.ExceptionHandled = true;
            ResultText.Text = "Cannot delete this question.  It is associated with answers recorded in the database.";
            ResultText.Visible = true;
        }

        protected void FormView1_DataBound(object sender, EventArgs e)
        {
            SetHelpText();
        }

        private void SetHelpText()
        {
            Label helpText = FormView1.FindControl("HelpText") as Label;
            DropDownList answerType = FormView1.FindControl("AnswerTypeDropdown") as DropDownList;
            if (helpText == null || answerType == null)
                return;
            AnswerObjectType aot = ActiveRecord<AnswerObjectType>.FetchByID(answerType.SelectedValue);
            if (!aot.IsLoaded) return;
            helpText.Text = aot.AnswerConfigurationHelpText;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // this behaved badly once; not sure if it is needed or not.
            //Query q = new Query(Tables.Measure);
            //object o = q.ExecuteScalar();
            //if (o == null)
            //{
            //    SetResultText("You must create a mesure first.  Please go to <a href=\"{0}\">Measures</a> and create a measure.",ResolveUrl("Measures.aspx"));
            //    return;
            //}
        }

        protected void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            GridView1.DataBind();
        }

        protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            GridView1.DataBind();
        }

        protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            GridView1.DataBind();
        }

        protected void AnswerTypeDropdown_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetHelpText();
        }

    }
}
